//
//  HomeHeaderView.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/20.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "HomeHeaderView.h"
#import "SDCycleScrollView.h"
#import "HomeCatesView.h"
@interface HomeHeaderView()<SDCycleScrollViewDelegate>
@property (nonatomic,strong)SDCycleScrollView *cycleView;
@property (nonatomic,strong)HomeCatesView *catesView;
@property (nonatomic,copy)NSArray *ads;
@end

@implementation HomeHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self getads];
    }
    return self;
}


-(void)reload{
    [self getAdRequest];
    [self.catesView getCatesRequest];
}
-(void)getAdRequest{
    
    [[TPRequestClient sharedClient]getAdList_success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
             _ads = [NSArray yy_modelArrayWithClass:[AdsModel class] json:respone.data[@"data"]];
            NSMutableArray *imgs = [NSMutableArray new];
            [_ads enumerateObjectsUsingBlock:^(AdsModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [imgs addObject:kGetMediaUrl(obj.img)];
            }];
          self.cycleView.imageURLStringsGroup = imgs;
          [self.catesView layoutIfNeeded];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        ;
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        ;
    }];
}
-(void)getads{
    
    
    [self getAdRequest];
    
    self.cycleView.imageURLStringsGroup = @[@"http://baidu.com",defaultUrl,@"http://baidu.com"];
    [self.catesView layoutIfNeeded];
    
    self.backgroundColor = [UIColor redColor];
    UIButton *hotButton = [UIButton buttonWithFrame:CGRectMake(0, self.catesView.bottom+5, IPHONE_WIDTH, 50.0) backgroundImageName:@"Home_header_hot_bg"];
    hotButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.backgroundColor = [UIColor whiteColor];
    [hotButton setTitleColor:TPBlack_333333 forState:UIControlStateNormal];
    [hotButton.titleLabel setFont:fontSystemOfSize(16)];
    [hotButton setTitle:@"为您推荐，美丽爆品" forState:UIControlStateNormal];
    [self addSubview:hotButton];
    
    self.height =  hotButton.bottom+8.0;
    
    
}

-(SDCycleScrollView *)cycleView{
    
    if(!_cycleView){
        //12:25
        _cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_WIDTH*0.5) delegate:self placeholderImage:ImageNamed(@"banner_bg")];
        _cycleView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_cycleView];
    }
    return _cycleView;
}

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    if(self.adBlock){
        self.adBlock(index, _ads[index]);
    }
    //    YPMainModel *model = self.ads[index];
    //    //    model.idField = [dict[@"id"] integerValue];
    //    YPInfoDetailViewController *infoDetail = [YPInfoDetailViewController new];
    //    infoDetail.model = model;
    //    //    infoDetail.isAd = YES;
    //    [self.navigationController pushViewController:infoDetail animated:YES];
    
}
-(HomeCatesView *)catesView{
    if(!_catesView){
        _catesView = [[HomeCatesView alloc]initWithFrame:CGRectMake(0, self.cycleView.bottom, IPHONE_WIDTH, 210.0)];
        [self addSubview:_catesView];
  }
    return _catesView;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.catesView.cateBlock = self.cateBlock;
}
@end

@implementation AdsModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper{
    return @{@"idField":@"id"};
}
@end


